<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: '消息id' })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" />

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, useTemplateRef } from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'

defineOptions({
    name: 'user/chat',
})

const { t } = useI18n()
const tableRef = useTemplateRef('tableRef')

const baTable = new baTableClass(
    new baTableApi('user/chat/'),
    {
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('Id'), prop: 'id', align: 'center', operator: '=', operatorPlaceholder: t('Id'), width: 70 },
            { label: '发送用户', prop: 'user.nickname', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: '接收用户', prop: 'touser.nickname', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: '接收用户', prop: 'content', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: t('Create time'), prop: 'createtime', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
            {
                label: t('Operate'),
                align: 'center',
                width: '130',
                render: 'buttons',
                buttons: defaultOptButtons(['edit', 'delete']),
                operator: false,
            },
        ],
        dblClickNotEditColumn: [undefined],
    },
    {},
    {},
    {
        getEditData({ res }) {
            if (res && res.data) {
                baTable.form.items = res.data
            }
        },
    }
)

// 利用提交前钩子重写提交操作
// baTable.before.onSubmit = ({ formEl, operate, items }) => {
//     let submitCallback = () => {
//         baTable.form.submitLoading = true
//         baTable.api
//             .postData(operate, {
//                 ...items,
//                 rules: formRef.value?.getCheckeds(),
//             })
//             .then((res) => {
//                 baTable.onTableHeaderAction('refresh', {})
//                 baTable.form.submitLoading = false
//                 baTable.form.operateIds?.shift()
//                 if (baTable.form.operateIds!.length > 0) {
//                     baTable.toggleForm('Edit', baTable.form.operateIds)
//                 } else {
//                     baTable.toggleForm()
//                 }
//                 baTable.runAfter('onSubmit', { res })
//             })
//             .catch(() => {
//                 baTable.form.submitLoading = false
//             })
//     }

//     if (formEl) {
//         baTable.form.ref = formEl
//         formEl.validate((valid) => {
//             if (valid) {
//                 submitCallback()
//             }
//         })
//     } else {
//         submitCallback()
//     }
//     return false
// }

// 打开表单后
// baTable.after.toggleForm = ({ operate }) => {
//     if (operate == 'Add') {
//         menuRuleTreeUpdate()
//     }
// }

// 获取到编辑数据后
// baTable.after.getEditData = () => {
//     menuRuleTreeUpdate()
// }

// const menuRuleTreeUpdate = () => {
//     getUserRules().then((res) => {
//         baTable.form.extend!.menuRules = res.data.list

//         if (baTable.form.items!.rules && baTable.form.items!.rules.length) {
//             if (baTable.form.items!.rules.includes('*')) {
//                 let arr: number[] = []
//                 for (const key in baTable.form.extend!.menuRules) {
//                     arr.push(baTable.form.extend!.menuRules[key].id)
//                 }
//                 baTable.form.extend!.defaultCheckedKeys = arr
//             } else {
//                 baTable.form.extend!.defaultCheckedKeys = baTable.form.items!.rules
//             }
//         } else {
//             baTable.form.extend!.defaultCheckedKeys = []
//         }
//         baTable.form.extend!.treeKey = uuid()
//     })
// }

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getData()
})
</script>

<style scoped lang="scss"></style>
